package com.i2works.smartluolong.manage.controller.monitor;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.manage.controller.common.CommonItemController;
import com.i2works.smartluolong.manage.service.admin.IAdminRoleService;
import com.i2works.smartluolong.utils.base.FlowItemTypeEnum;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.flow.FlowInfoQuery;
import com.i2works.smartluolong.web.service.flow.IFlowInfoService;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 监控报表 - 事项办理列表控制层
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/6/5 13:52
 */

@Controller
@RequestMapping("/monitorItemList")
public class MonitorItemListController extends CommonItemController {


    private final IFlowInfoService flowInfoService;

    public MonitorItemListController(IAdminRoleService adminRoleService, IFlowTemplateService flowTemplateService, IFlowService flowService, IFlowProcessService flowProcessService, IFlowInfoService flowInfoService) {
        super(adminRoleService, flowTemplateService, flowService, flowProcessService);
        this.flowInfoService = flowInfoService;
    }

    /**
     * 申请页面
     */
    @GetMapping(value = "/view/index")
    public String index(Model model) {
        model.addAttribute("itemType", FlowItemTypeEnum.values());
        logger.info("事项办理列表页面");
        return "/monitor/list_index";
    }

    /**
     * 申请列表
     */
    @GetMapping(value = "/view/api")
    @ResponseBody
    public Result queryList(@ModelAttribute FlowInfoQuery query) {
        try {
            if (StringUtils.isBlank(query.getItemType())) {
                query.setItemType(null);
            }
            //查询列表统计
            query.setComplete(0);
            Integer total = flowInfoService.queryCount(query);
            List<FlowInfoDO> flowInfoList = Lists.newArrayList();
            if (total > 0) {
                flowInfoList = flowInfoService.queryListByMonitor(query);
            }
            return R.success(flowInfoList, total);
        } catch (Exception e) {
            logger.error("事项办理列表API异常：", e);
            return R.error(MsgEnum.SYSTEM_ERROR.getMsg());
        }
    }


    /**
     * 申请页面
     */
    @GetMapping(value = "/view/{itemType}/{itemId}")
    public String infoForm(@PathVariable String itemType, @PathVariable Integer itemId, Model model) {
        try {
            model.addAttribute("flowProcessList", queryFlowProcess(itemId, itemType));
        } catch (ServiceException e) {
            logger.error("事项办理详情异常：", e);
        }
        return "/monitor/list_form";
    }


}
